package com.eorchis.module.basedata.ui.controller;

import com.eorchis.core.page.commond.JSONObject;
import com.eorchis.core.ui.token.IToken;
import com.eorchis.module.ModelBeanUtils;
import com.eorchis.module.basedata.domain.BaseData;
import com.eorchis.module.basedata.domain.BaseDataCondition;
import com.eorchis.module.basedata.domain.BaseDataType;
import com.eorchis.module.basedata.domain.BaseDataTypeCondition;
import com.eorchis.module.basedata.service.IBaseDataService;
import com.eorchis.module.basedata.service.IBaseDataTypeService;
import com.eorchis.module.basedata.ui.commond.BaseDataCommond;
import com.eorchis.module.basedata.ui.commond.BaseDataValidCommond;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("baseDataController")
@RequestMapping({"/module/basedata"})
public class BaseDataController
{
  public static final String modulePath = "/module/basedata";

  @Autowired
  @Qualifier("com.eorchis.module.basedata.service.impl.BaseDataServiceImpl")
  private IBaseDataService iBaseDataService;

  @Autowired
  @Qualifier("com.eorchis.module.basedata.service.impl.BaseDataTypeServiceImpl")
  private IBaseDataTypeService baseDataTypeService;

  @Autowired
  private IToken token;

  @RequestMapping({"/listBaseData"})
  public String listBaseData(Model model, BaseDataCommond baseDataCommond)
    throws Exception
  {
    BaseDataCondition condition = new BaseDataCondition();
    BeanUtils.copyProperties(baseDataCommond, condition);
    if ((condition.getSearchActiveState() == null) || ("".equals(condition.getSearchActiveState())))
    {
      condition.setSearchActiveState(BaseData.IS_ACTIVE_Y.toString());
    }
    JSONObject result = this.iBaseDataService.doProcess(baseDataCommond, condition);
    ModelBeanUtils.jsonObjectToModel(result, model);
    return "/module/basedata/list";
  }

  @RequestMapping({"/listBaseDataByTypeCode"})
  public String listBaseDataByTypeCode(Model model, BaseDataCommond baseDataCommond, HttpServletRequest request)
    throws Exception
  {
    BaseDataTypeCondition baseDataTypeCondition = new BaseDataTypeCondition();
    baseDataTypeCondition.setSearchParentTypeCode(baseDataCommond.getSearchBaseTypeCode());
    List<BaseDataType> bdtList = this.baseDataTypeService.listBaseDataType(baseDataTypeCondition);

    baseDataCommond.setSearchActiveState(BaseData.IS_ACTIVE_Y.toString());
    BaseDataCondition condition = new BaseDataCondition();
    if ((bdtList != null) && (bdtList.size() > 0)) {
      String[] baseDateTypeIds = new String[bdtList.size()];
      for (int i = 0; i < bdtList.size(); i++) {
        baseDateTypeIds[i] = ((BaseDataType)bdtList.get(i)).getTypeID();
      }
      condition.setSearchDataTypeIDs(baseDateTypeIds);
    } else {
      BeanUtils.copyProperties(baseDataCommond, condition);
    }
    List<BaseData> list = this.iBaseDataService.getBaseDataList(condition);
    List resultList = new ArrayList();

    for (BaseData baseData : list) {
      for (BaseDataType bdt : bdtList) {
        if (bdt.getTypeID() == baseData.getBaseDataType().getTypeID()) {
          baseData.setDataName(bdt.getTypeName() + "-" + baseData.getDataName());
        }
      }

      resultList.add(baseData);
    }
    ModelBeanUtils.jsonObjectToModel(model, true, null, resultList);
    return "";
  }

  @RequestMapping({"/saveToken"})
  @ResponseBody
  public String preAddBaseData(HttpServletRequest request)
    throws Exception
  {
    String tokenStr = this.token.saveToken(request);
    return tokenStr;
  }

  @RequestMapping({"/addBaseData"})
  public String addBaseData(Model model, HttpServletRequest request, BaseDataValidCommond baseDataValidCommond)
    throws Exception
  {
    BaseData baseData = null;

    if (this.token.isTokenValid(request, true)) {
      baseData = new BaseData();
      if (baseDataValidCommond.getSearchDataTypeID() != null) {
        BeanUtils.copyProperties(baseDataValidCommond, baseData);
        BaseDataType baseDataType = new BaseDataType();
        baseDataType.setTypeID(baseDataValidCommond.getSearchDataTypeID());
        baseData.setBaseDataType(baseDataType);
        baseData.setCreateTime(new Date());
        this.iBaseDataService.addBaseData(baseData);
        ModelBeanUtils.jsonObjectToModel(model, true, "添加成功！", baseData);
      } else {
        ModelBeanUtils.jsonObjectToModel(model, false, "添加失败！", null);
      }
    }
    return "forward:/module/basedata/listBaseData" + ModelBeanUtils.getRequestType(request);
  }

  @RequestMapping({"/preUpdateBaseData"})
  public String preUpdateBaseData(Model model, BaseDataValidCommond baseDataValidCommond)
    throws Exception
  {
    BaseDataCondition baseDataCondition = null;
    BaseData baseData = null;

    if ((baseDataValidCommond.getSearchDataId() != null) && (!"".equals(baseDataValidCommond.getSearchDataId()))) {
      baseDataCondition = new BaseDataCondition();
      baseDataCondition.setSearchDataId(baseDataValidCommond.getSearchDataId());
      baseData = this.iBaseDataService.getBaseData(baseDataCondition);
      BeanUtils.copyProperties(baseData, baseDataValidCommond);
      if (baseData.getBaseDataType() != null) {
        baseDataValidCommond.setSearchDataTypeID(baseData.getBaseDataType().getTypeID());
      }

      ModelBeanUtils.jsonObjectToModel(model, true, null, baseDataValidCommond);
    }
    return "/module/basedata/view";
  }

  @RequestMapping({"/updataBaseData"})
  public String updataBaseData(Model model, HttpServletRequest request, BaseDataValidCommond baseDataValidCommond)
    throws Exception
  {
    BaseData baseData = null;

    if (this.token.isTokenValid(request, true)) {
      baseData = new BaseData();
      BeanUtils.copyProperties(baseDataValidCommond, baseData);
      if (baseDataValidCommond.getSearchDataTypeID() != null) {
        BaseDataType baseDataType = new BaseDataType();
        baseDataType.setTypeID(baseDataValidCommond.getSearchDataTypeID());
        baseData.setBaseDataType(baseDataType);
        this.iBaseDataService.updateBaseData(baseData);
        ModelBeanUtils.jsonObjectToModel(model, true, "修改成功！", baseData);
      } else {
        ModelBeanUtils.jsonObjectToModel(model, false, "基础数据类别为空！", null);
      }
    }
    return "forward:/module/basedata/listBaseData" + ModelBeanUtils.getRequestType(request);
  }

  @RequestMapping({"/checkDataCodeIsRepeat"})
  public String checkDataCodeIsRepeat(Model model, BaseDataValidCommond baseDataValidCommond)
    throws Exception
  {
    if ((baseDataValidCommond.getDataCode() != null) && (!"".equals(baseDataValidCommond.getDataCode()))) {
      BaseData baseData = new BaseData();
      baseData.setDataCode(baseDataValidCommond.getDataCode());
      baseData.setDataID(baseDataValidCommond.getDataID());
      boolean check = this.iBaseDataService.checkDataCodeIsRepeat(baseData);
      if (check)
      {
        ModelBeanUtils.jsonObjectToModel(model, false, "编码重复", null);
      }
      else ModelBeanUtils.jsonObjectToModel(model, true, null, null); 
    }
    else
    {
      ModelBeanUtils.jsonObjectToModel(model, false, "查询数据编码为空！", null);
    }

    return "";
  }

  @RequestMapping({"/discardOrReuseBaseData"})
  public String discardOrReuseBaseData(Model model, HttpServletRequest request, BaseDataCommond baseDataCommond)
    throws Exception
  {
    if ((baseDataCommond.getSearchActive() != null) && (!"".equals(baseDataCommond.getSearchActive())) && (baseDataCommond.getBaseDataIds() != null) && (baseDataCommond.getBaseDataIds().length > 0)) {
      BaseDataCondition baseDataCondition = new BaseDataCondition();

      BeanUtils.copyProperties(baseDataCommond, baseDataCondition);
      this.iBaseDataService.discardOrReuseBaseData(baseDataCondition);
      ModelBeanUtils.jsonObjectToModel(model, true, "修改成功！", null);
    } else {
      ModelBeanUtils.jsonObjectToModel(model, false, "为空", null);
    }
    return "forward:/module/basedata/listBaseData" + ModelBeanUtils.getRequestType(request);
  }
}